package com.kingdee.pda.ui.interception_registr;

import android.content.Context;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.kingdee.pda.R;
import com.kingdee.pda.callback.OnCheckBoxChangeListener;

import java.util.List;

/**
 * @author：dlj
 * @data：2022/5/6
 * @des：
 */
public class InterceptionRegistrListAdapter extends BaseQuickAdapter<InterceptionOrderBean, BaseViewHolder> {

    private final OnCheckBoxChangeListener onCheckBoxChangeListener;
    private Context context;
    private int opinionPosition = -1;

    public void setOpinionPosition(int opinionPosition) {
        this.opinionPosition = opinionPosition;
    }

    public int getOpinionPosition() {
        return opinionPosition;
    }

    public InterceptionRegistrListAdapter(Context context, List<InterceptionOrderBean> beans, OnCheckBoxChangeListener onCheckBoxChangeListener) {
        super(R.layout.item_interception_registr_list, beans);
        this.context = context;
        this.onCheckBoxChangeListener = onCheckBoxChangeListener;
    }

    @Override
    protected void convert(BaseViewHolder viewHolder, InterceptionOrderBean item) {
        viewHolder.setText(R.id.tv_billNo, item.getBillNo())
                .setText(R.id.tv_mailNo, "物流单号：" + item.getMailNo())
                .setText(R.id.tv_skuQty, "SKU数量：" + item.getSkuQty())
                .setText(R.id.tv_time, "创建时间：" + item.getCreateTime());

        CheckBox checkBox = viewHolder.getView(R.id.checkBox);
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                //点击已选中的checkbox，不做处理
                if (!b && opinionPosition == viewHolder.getLayoutPosition()) {
                    compoundButton.setChecked(true);
                    return;
                }
                if (b) {
                    onCheckBoxChangeListener.onCheckChanged(viewHolder.getLayoutPosition(), true);
                    opinionPosition = viewHolder.getLayoutPosition();
                    notifyDataSetChanged();
                }
            }
        });

        if (opinionPosition != viewHolder.getLayoutPosition()) {
            checkBox.setChecked(false);
        }
    }
}
